package com.yewen.http.controller;

import com.alibaba.fastjson.JSONObject;
import com.yewen.http.core.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Classname RankingHandler
 * @Description 刷新高校排名请求处理
 * @Date 2022/4/11 16:57
 * @Created by TaoVh
 */
@RestController
@RequestMapping("/yewen/ranking")
public class RankingHandler {
    @Autowired
    @Qualifier("redisTemplate")
    RedisTemplate redisTemplate;

    @GetMapping("/getUniversityRanking")
    public CommonResult getUniversityRanking(){
        LinkedHashSet<ZSetOperations.TypedTuple<String>> rankingSet = (LinkedHashSet<ZSetOperations.TypedTuple<String>>) redisTemplate.opsForZSet().reverseRangeWithScores("universityRanking",0,4);
        JSONObject data = new JSONObject(new LinkedHashMap<>());
        for(ZSetOperations.TypedTuple<String> tuple:rankingSet){
            data.put(tuple.getValue(),tuple.getScore());
        }
        return CommonResult.success(data.toJSONString());
    }
    @GetMapping("/getMajorRanking")
    public CommonResult getMajorRanking(){
        LinkedHashSet<ZSetOperations.TypedTuple<String>> rankingSet = (LinkedHashSet<ZSetOperations.TypedTuple<String>>) redisTemplate.opsForZSet().reverseRangeWithScores("majorRanking",0,4);
        JSONObject data = new JSONObject(new LinkedHashMap<>());
        for(ZSetOperations.TypedTuple<String> tuple:rankingSet){
            data.put(tuple.getValue(),tuple.getScore());
        }
        return CommonResult.success(data.toJSONString());
    }

    @PostMapping("/contributeMajor")
    public void contributeMajorRanking(String majorName,String decoratedUserName){
        boolean isExist = redisTemplate.hasKey(decoratedUserName);
        if (!isExist){
            redisTemplate.opsForHyperLogLog().add(decoratedUserName,majorName);
            redisTemplate.expire(decoratedUserName,180, TimeUnit.SECONDS);
            redisTemplate.opsForZSet().incrementScore("majorRanking",majorName,1);
        }else {
            long l = redisTemplate.opsForHyperLogLog().add(decoratedUserName,majorName);
            /*180s内该用户还未搜索过该高校*/
            if (l==1){
                redisTemplate.opsForZSet().incrementScore("majorRanking",majorName,1);
            }
        }
    }
    @PostMapping("/contributeUniversity")
    public void contributeUniversityRanking(String universityName,String decoratedUserName){
        boolean isExist = redisTemplate.hasKey(decoratedUserName);
        if (!isExist){
            redisTemplate.opsForHyperLogLog().add(decoratedUserName,universityName);
            redisTemplate.expire(decoratedUserName,180, TimeUnit.SECONDS);
            redisTemplate.opsForZSet().incrementScore("universityRanking",universityName,1);
        }else {
            long l = redisTemplate.opsForHyperLogLog().add(decoratedUserName,universityName);
            /*180s内该用户还未搜索过该高校*/
            if (l==1){
                redisTemplate.opsForZSet().incrementScore("universityRanking",universityName,1);
            }
        }
    }

}
